//
//  OrderdetailFooter.swift
//  Eternity
//
//  Created by cll12_12 on 2016/12/23.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit
import SnapKit

class OrderdetailFooter: UITableViewHeaderFooterView {

    var rightConstraint: Constraint?
    
    // MAKR: - life cycle
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        
        initBaseLayout()
        layoutPageSubviews()
        createUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - private method
    func initBaseLayout() {
        self.addSubview(topBackView)
        self.addSubview(totalNumLabel)
        self.addSubview(middleSpace)
        self.addSubview(bottomBackView)
        self.addSubview(middleLine)
        self.addSubview(realPaymentLabel)
        self.addSubview(orderTime)
        self.addSubview(serviceLabel)
        self.addSubview(bottomLine)
        self.addSubview(checkLogisticsBtn)
        self.addSubview(confirmBtn)
    }
    
    func layoutPageSubviews() {
        topBackView.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(0)
            make.top.equalTo(self).offset(0)
            make.right.equalTo(self).offset(0)
            make.height.equalTo(40)
        }
        
        totalNumLabel.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-14)
            make.centerY.equalTo(topBackView)
            make.width.equalTo(76)
            make.height.equalTo(20)
        }
        
        middleSpace.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(0)
            make.top.equalTo(topBackView.snp.bottom).offset(0)
            make.right.equalTo(self).offset(0)
            make.height.equalTo(16)
        }
        
        bottomBackView.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(0)
            make.bottom.equalTo(self).offset(0)
            make.right.equalTo(self).offset(0)
            make.height.equalTo(283)
        }
        
        middleLine.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(0)
            make.top.equalTo(bottomBackView).offset(145)
            make.right.equalTo(self).offset(0)
            make.height.equalTo(1)
        }
        
        realPaymentLabel.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-14)
            make.top.equalTo(middleLine.snp.bottom).offset(12)
            make.width.equalTo(176)
            make.height.equalTo(20)
        }
        
        orderTime.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-14)
            make.top.equalTo(realPaymentLabel.snp.bottom).offset(11)
            make.width.equalTo(161)
            make.height.equalTo(20)
        }
        
        serviceLabel.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-14)
            make.top.equalTo(orderTime.snp.bottom).offset(2)
            make.width.equalTo(153)
            make.height.equalTo(14)
        }
        
        bottomLine.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(0)
            make.bottom.equalTo(self).offset(-48)
            make.right.equalTo(self).offset(0)
            make.height.equalTo(1)
        }
        
        checkLogisticsBtn.snp.makeConstraints { (make) in
            rightConstraint = make.right.equalTo(self).offset(-108).constraint
            make.top.equalTo(bottomLine.snp.bottom).offset(8)
            make.width.equalTo(84)
            make.height.equalTo(32)
        }
        
        confirmBtn.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-14)
            make.top.equalTo(bottomLine.snp.bottom).offset(8)
            make.width.equalTo(84)
            make.height.equalTo(32)
        }
    }
    
    func createUI() {
        let dataSource = [["商品总额：", "配送费：", "HARMAY积分：", "优惠促销："], ["￥459.00", "￥0.00", "-￥5.00", "-￥20.00"]]
        for i in 0 ..< dataSource[0].count {
            let contentLabel = UILabel(frame: CGRect(x: 14, y: 20+i*30, width: 110, height: 20))
            contentLabel.font = UIFont(name: "PingFangSC-Regular", size: 14)
            contentLabel.textColor = UIColor(hexString: "#252525")
            contentLabel.text = dataSource[0][i]
            
            let priceLabel = UILabel(frame: CGRect(x: Int(SCREEN_WIDTH-114), y: 20+i*30, width: 100, height: 20))
            priceLabel.font = UIFont(name: "PingFangSC-Regular", size: 14)
            priceLabel.textColor = UIColor(hexString: "#000000")
            priceLabel.textAlignment = .right
            priceLabel.text = dataSource[1][i]
            
            bottomBackView.addSubview(contentLabel)
            bottomBackView.addSubview(priceLabel)
        }
    }
    
    func configUI(status: STATUS) {
        totalNumLabel.text = "共3件商品"
        realPaymentLabel.text = "实付款：￥434.00"
        orderTime.text = "下单时间：2016.12.23 16:56"
        
        
        switch status {
        case .WAITFORPAY:
            checkLogisticsBtn.setTitle("取消订单", for: .normal)
            confirmBtn.setTitle("去支付", for: .normal)
            break
        case .WAITFORGOODS:
            checkLogisticsBtn.setTitle("查看物流", for: .normal)
            confirmBtn.setTitle("确认收货", for: .normal)
            break
        case .PAID:
            checkLogisticsBtn.setTitle("取消订单", for: .normal)
            confirmBtn.isHidden = true
            rightConstraint?.update(inset: 14)
            break
        case .CANCELED:
            confirmBtn.isHidden = true
            checkLogisticsBtn.isHidden = true
            break
        case .FINISHED:
            checkLogisticsBtn.setTitle("再次购买", for: .normal)
            confirmBtn.setTitle("申请售后", for: .normal)
            break
        default:
            break
        }
        
        
    }
    
    // MARK: - setters and getters
    var _topBackView: UIView!
    var topBackView: UIView {
        if _topBackView == nil {
            _topBackView = UIView()
            _topBackView.backgroundColor = UIColor.white
        }
        return _topBackView
    }
    
    var _totalNumLabel: UILabel!
    var totalNumLabel: UILabel {
        if _totalNumLabel == nil {
            _totalNumLabel = UILabel()
            _totalNumLabel.font = UIFont(name: "PingFangSC-Regular", size: 14)
            _totalNumLabel.textColor = UIColor(hexString: "#000000")
            _totalNumLabel.textAlignment = .right
        }
        return _totalNumLabel
    }
    
    var _middleSpace: UIView!
    var middleSpace: UIView {
        if _middleSpace == nil {
            _middleSpace = UIView()
            _middleSpace.backgroundColor = UIColor(hexString: "#EEEEEE")
        }
        return _middleSpace
    }
    
    var _bottomBackView: UIView!
    var bottomBackView: UIView {
        if _bottomBackView == nil {
            _bottomBackView = UIView()
            _bottomBackView.backgroundColor = UIColor.white
        }
        return _bottomBackView
    }
    
    var _middleLine: UIView!
    var middleLine: UIView {
        if _middleLine == nil {
            _middleLine = UIView()
            _middleLine.backgroundColor = UIColor(hexString: "#D1D1D1")
        }
        return _middleLine
    }
    
    var _realPaymentLabel: UILabel!
    var realPaymentLabel: UILabel {
        if _realPaymentLabel == nil {
            _realPaymentLabel = UILabel()
            _realPaymentLabel.font = UIFont(name: "PingFangSC-Medium", size: 14)
            _realPaymentLabel.textColor = UIColor(hexString: "#252525")
            _realPaymentLabel.textAlignment = .right
        }
        return _realPaymentLabel
    }
    
    var _orderTime: UILabel!
    var orderTime: UILabel {
        if _orderTime == nil {
            _orderTime = UILabel()
            _orderTime.font = UIFont(name: "PingFangSC-Regular", size: 12)
            _orderTime.textColor = UIColor(hexString: "#999999")
            _orderTime.textAlignment = .right
        }
        return _orderTime
    }
    
    var _serviceLabel: UILabel!
    var serviceLabel: UILabel {
        if _serviceLabel == nil {
            _serviceLabel = UILabel()
            _serviceLabel.font = UIFont(name: "PingFangSC-Regular", size: 12)
            _serviceLabel.textColor = UIColor(hexString: "#999999")
            _serviceLabel.textAlignment = .right
            _serviceLabel.text = "售后服务：400 890 6878"
        }
        return _serviceLabel
    }
    
    var _bottomLine: UIView!
    var bottomLine: UIView {
        if _bottomLine == nil {
            _bottomLine = UIView()
            _bottomLine.backgroundColor = UIColor(hexString: "#D1D1D1")
        }
        return _bottomLine
    }
    
    var _checkLogisticsBtn: UIButton!
    var checkLogisticsBtn: UIButton {
        if _checkLogisticsBtn == nil {
            _checkLogisticsBtn = UIButton()
            _checkLogisticsBtn.backgroundColor = UIColor.white
            _checkLogisticsBtn.layer.borderWidth = 0.5
            _checkLogisticsBtn.layer.borderColor = UIColor(hexString: "#B7B7B7")?.cgColor
            _checkLogisticsBtn.titleLabel?.font = UIFont(name: "PingFangSC-Regular", size: 12)
            _checkLogisticsBtn.setTitleColor(UIColor(hexString: "#252525"), for: .normal)
        }
        return _checkLogisticsBtn
    }
    
    var _confirmBtn: UIButton!
    var confirmBtn: UIButton {
        if _confirmBtn == nil {
            _confirmBtn = UIButton()
            _confirmBtn.backgroundColor = UIColor(hexString: "#252525")
            _confirmBtn.layer.borderWidth = 0.5
            _confirmBtn.layer.borderColor = UIColor(hexString: "#B7B7B7")?.cgColor
            _confirmBtn.titleLabel?.font = UIFont(name: "PingFangSC-Regular", size: 12)
            _confirmBtn.setTitleColor(UIColor(hexString: "#FFFFFF"), for: .normal)
        }
        return _confirmBtn
    }
    

}
